//
//  SquareViewController.m
//  A_GOGO
//
//  Created by Liuiu on 16/1/16.
//  Copyright © 2016年 www.lanou3g.com. All rights reserved.
//

#import "SquareViewController.h"
#import "UIColor+Wonderful.h"
#import "NetworkHandler.h"
#import <UIImageView+WebCache.h>
#import "HomeModel.h"
#import "SquareCollectionViewCell.h"
#import "MoreViewController.h"
#import <MMDrawerBarButtonItem.h>
#import <UIViewController+MMDrawerController.h>
#import "LeftDrawerViewController.h"
@interface SquareViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate>
@property (nonatomic, retain)UICollectionView *collectionV;//热门搜索模块
@property (nonatomic, retain)NSDictionary *dic; //放两个模块的图
@property (nonatomic, retain)NSMutableArray *arr;//放游戏图
@property (nonatomic, retain)UIScrollView *scrollView;//用于"热门搜索"
@end

@implementation SquareViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //导航栏
    self.view.backgroundColor =[UIColor whiteColor];
    self.navigationItem.title =@"广场";
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:21],
       NSForegroundColorAttributeName:[UIColor grayColor]}];
    self.navigationController.navigationBar.translucent = NO;
    
    //个人资料
    UIButton *buttona = [UIButton buttonWithType:UIButtonTypeSystem];
    buttona.backgroundColor = [UIColor whiteColor];
    buttona.frame =  CGRectMake(0, 0, 38, 38);
    buttona.layer.cornerRadius = 19;
    [buttona setBackgroundImage:[UIImage imageNamed:@"personal"] forState:UIControlStateNormal];
    [buttona addTarget:self action:@selector(left:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttona];
    
    //搜索框
    UITextField *field =[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 220, 32)];
    field.backgroundColor = [UIColor gainsboroColor];
    field.layer.cornerRadius = 7;
    field.placeholder = @"                 搜索游戏";
    
    //textField清除功能
    field.clearButtonMode = UITextFieldViewModeAlways;
    self.navigationItem.titleView = field;
    [field release];
    
    //a Label
    UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(15, 395, 100, 30)];
    label.text = @"热门搜索";
    label.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    [self.view addSubview:label];
    [label release];
    
//******** scrollview
    self.scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(5, 420, self.view.frame.size.width , 110)];
   
    _scrollView.delegate = self;
    _scrollView.backgroundColor = [UIColor whiteColor];
    self.scrollView.contentSize = CGSizeMake(375.5,0);
    _scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_scrollView];
    [_scrollView release];
//
//********* Collection
    UICollectionViewFlowLayout *layout =[[UICollectionViewFlowLayout alloc]init];
    
    //滚动方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    //横向距离
    layout.minimumLineSpacing = 25;
    
    //每个cell大小
    layout.itemSize = CGSizeMake((285 / 4.5), (285 / 4.5));
    
    //边界
    layout.sectionInset =UIEdgeInsetsMake(0, 10, 0, 10);
    
    //创建
    self.collectionV=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width * 2, 110) collectionViewLayout:layout];
    _collectionV.backgroundColor =[UIColor whiteColor];
    _collectionV.delegate = self;
    _collectionV.dataSource =self;
    [self.scrollView addSubview:_collectionV];
    [_collectionV release];
    
    //注册重用池
    [_collectionV registerClass:[SquareCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    

    [self data];

    
    
    
    
    
}
- (void)data
{
//******   两个模块
    NSString *str =@"http://open.youxiduo.com/plaza/home?debug=beta&source=e21be6a6099d1dc4ad63e0d5c8b1d6dc&appname=yxdjqb&os=ios&version=3.4.0";
    [NetworkHandler GETWithURL:str completion:^(id result) {
        self.dic =result[@"result"];
        self.arr =result[@"result"][@"hotSearch"];
        self.arr =[HomeModel modelHanderWithArray:_arr];
        [_collectionV reloadData];
        HomeModel *n =[[HomeModel alloc]init];
        [n  setValuesForKeysWithDictionary:_dic];
//******  热门排行
        UIImageView *img =[[UIImageView alloc]initWithFrame:CGRectMake(10, 20, self.view.frame.size.width - 20, 150)];
        img.layer.cornerRadius = 7;
        
        [img sd_setImageWithURL:[NSURL URLWithString:n.titleImage]];
        [self.view addSubview:img];
        [img release];
        
        UIButton *button =[UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(10, 20, self.view.frame.size.width - 20, 150) ;
        button.backgroundColor =[UIColor clearColor];
        [button addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
//******  探索星座
        UIImageView *imgb =[[UIImageView alloc]initWithFrame:CGRectMake(10, 190, self.view.frame.size.width - 20, 150)];
        imgb.layer.cornerRadius = 7;
        
        [imgb sd_setImageWithURL:[NSURL URLWithString:n.signImage]];
        [self.view addSubview:imgb];
        [imgb release];
        
        UIButton *buttont =[UIButton buttonWithType:UIButtonTypeSystem];
        buttont.frame = CGRectMake(10, 190, self.view.frame.size.width - 20, 150) ;
        buttont.backgroundColor =[UIColor clearColor];
        [buttont addTarget:self action:@selector(Clickb:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:buttont];
        
    }];
    
    
    
    
}
//实现collection两个方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SquareCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    HomeModel *m =_arr[indexPath.row];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:m.img]];
    cell.title.text =m.title;
    
    return cell;
}
#pragma mark - Button区
- (void)Click:(UIButton *)btn
{
    MoreViewController *sec =[[MoreViewController alloc]init];
    [self.navigationController pushViewController:sec animated:YES];
}
- (void)Clickb:(UIButton *)btn
{
    NSLog(@"瞅你咋地");
}
- (void)left:(UIBarButtonItem *)btn
{
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:^(BOOL finished) {
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
